package com.hudomju.swipe.sample;

import com.hudomju.swipe.interfaces.BaseModal;
import com.hudomju.swipe.widget.ItemLayout;
import com.hudomju.swipe.adapter.SwipeDirection;
import com.hudomju.swipe.adapter.BaseAdapter;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import ohos.app.Context;

import java.util.ArrayList;

/**
 * RecyclerView页面
 */
public class RecyclerViewAbility extends Ability implements BaseAdapter.Callbacks {
    private static final int TIME_TO_AUTOMATICALLY_DISMISS_ITEM = 3000;
    private DirectionalLayout toastLayout;
    private BaseAdapter listItemProvider;
    private ArrayList<BaseModal> mDataSet;

    @Override
    protected void onStart(Intent intent) {
        getWindow().setStatusBarColor(Color.rgb(185,53,40));
        getWindow().setStatusBarVisibility(Component.VISIBLE);

        super.onStart(intent);
        setUIContent(ResourceTable.Layout_recycler_view_ability);
        init((ListContainer) findComponentById(ResourceTable.Id_recycler_view));
    }

    private void init(ListContainer listView) {
        mDataSet = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mDataSet.add(new BaseModal("This is row number " + i, false));
        }

        listItemProvider = new MyAdapter(getContext(), mDataSet, this);
        listView.setItemProvider(listItemProvider);
    }

    @Override
    public boolean hasActions(int position, SwipeDirection direction) {
        if (direction.isLeft()) {
            return true;
        }
        if (direction.isRight()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean shouldDismiss(int position, SwipeDirection direction) {
        return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;
    }

    @Override
    public void onSwipe(int position, SwipeDirection direction) {
        switch (direction) {
            case DIRECTION_FAR_LEFT:
            case DIRECTION_FAR_RIGHT:
                listItemProvider.changeBackground(position);
                listItemProvider.setDismissDelay(TIME_TO_AUTOMATICALLY_DISMISS_ITEM);
                listItemProvider.dismissDelayItem(position);
                break;
            case DIRECTION_NORMAL_LEFT:
            case DIRECTION_NORMAL_RIGHT:
                break;
        }
    }

    @Override
    public void onItemClicked(int position) {
        toastLayout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text text = (Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast);
        text.setText("Position " + position);
        new ToastDialog(getContext())
                .setContentCustomComponent(toastLayout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setTransparent(true)
                .setAlignment(LayoutAlignment.BOTTOM)
                .setOffset(0, 200)
                .show();
    }

    @Override
    public void onSwipeStarted(ListContainer listView, int position, SwipeDirection direction) {
    }

    @Override
    public void onSwipeEnded(ListContainer listView, int position, SwipeDirection direction) {
    }

    private static class MyAdapter extends BaseAdapter {
        MyAdapter(Context context, ArrayList<BaseModal> data, Callbacks callbacks) {
            super(context, data, callbacks);
        }

        @Override
        protected int getLayoutResId() {
            return ResourceTable.Layout_recycler_list_item;
        }

        @Override
        protected int[] bindView() {
            return new int[]{ResourceTable.Id_lyt_container, ResourceTable.Id_layout_old,
                    ResourceTable.Id_layout_new, ResourceTable.Id_txt_data,
                    ResourceTable.Id_txt_delete, ResourceTable.Id_txt_undo};
        }

        @Override
        protected void initData(ViewHolder holder, BaseModal data, int position, Callbacks callbacks) {
            ItemLayout itemLayout = (ItemLayout) holder.getViewById(ResourceTable.Id_lyt_container);
            itemLayout.setCallBack(callbacks);
            itemLayout.setPosition(position);

            Text tvIndex = (Text) holder.getViewById(ResourceTable.Id_txt_data);
            tvIndex.setText(data.getIndex());

            Text tvDelete = (Text) holder.getViewById(ResourceTable.Id_txt_delete);
            Text tvUndo = (Text) holder.getViewById(ResourceTable.Id_txt_undo);

            tvDelete.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    setRecoveryPosition(position);
                    remove(position);
                }
            });

            tvUndo.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    setRecoveryPosition(position);
                    recoveryBackground(position);
                }
            });

            DirectionalLayout layoutOld = (DirectionalLayout) holder.getViewById(ResourceTable.Id_layout_old);
            DirectionalLayout layoutNew = (DirectionalLayout) holder.getViewById(ResourceTable.Id_layout_new);

            layoutOld.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    callbacks.onItemClicked(position);
                }
            });

            if (data.getShowBack()) {
                layoutOld.setVisibility(Component.INVISIBLE);
                layoutNew.setVisibility(Component.VISIBLE);
            } else {
                layoutOld.setVisibility(Component.VISIBLE);
                layoutNew.setVisibility(Component.INVISIBLE);
            }
        }
    }
}
